package com.gengzp.system.facade.impl;

import com.gengzp.common.exception.BusinessException;
import com.gengzp.common.model.role.entity.SysRole;
import com.gengzp.system.facade.ISysRoleFacade;
import com.gengzp.system.mapper.SysRoleMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName SysRoleFacadeImpl
 * @Description 系统角色门面层实现类
 * @Author gengzp
 * @Date 2025/5/17 2:14
 */
@Service
public class SysRoleFacadeImpl implements ISysRoleFacade {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public SysRole searchSysRoleById(Long roleId) {
        if (Objects.isNull(roleId)) {
            throw BusinessException.get("角色id不能为空");
        }
        return sysRoleMapper.selectSysRoleById(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editSysRolePermissions(Long roleId, List<String> permissionCodes) {
        if (Objects.isNull(roleId)) {
            throw BusinessException.get("角色id不能为空");
        }
        sysRoleMapper.updateSysRolePermissions(roleId, permissionCodes.stream().distinct().collect(Collectors.joining(",")));
    }

    @Override
    public List<SysRole> searchSysRoleByIds(List<Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            throw BusinessException.get("角色id不能为空");
        }
        return sysRoleMapper.selectSysRolesByIds(roleIds);
    }

    @Override
    public List<SysRole> searchAllSysRoles() {
        return sysRoleMapper.selectAllSysRoles();
    }

}
